<template>
    <el-row class="user-info-form" type="flex" justify="center">
        <el-col>
			<div class="add-attribute-wrap">
				<el-form ref="attributeForm" :model="attributeForm" :rules="attributeFormRules" @submit.native.prevent>
					<el-form-item label="名称：" prop='name'>
						<el-input v-model="attributeForm.name" auto-complete="off" placeholder="请输入名称">
						</el-input>
					</el-form-item>
					<el-form-item label="属性字段：" prop='code'>
						<el-input v-model="attributeForm.code" auto-complete="off" placeholder="请输入属性字段">
						</el-input>
					</el-form-item>
					<el-form-item label="单位：" prop='unit'>
						<el-select v-model="attributeForm.unit" filterable 
							placeholder="请选择"
							@change="changeItem">
							<el-option
								v-for="(value, key, index) in options"
								:key="index"
								:label="value"
								:value="value">
							</el-option>
						</el-select>
					</el-form-item> 
					<el-form-item label-width="0" class="verifyidentity-button">
						<el-button @click="closeAddFun" round>取消</el-button>
						<el-button type="primary" @click="saveLinkAttr('attributeForm')" round>提交</el-button>
					</el-form-item>
				</el-form>
			</div>
        </el-col>
    </el-row>
</template>
<script>
import utils from '@/utils/util'
import { getDictByCode } from '@/api/dic'
import { updateFcfNormalP } from '@/api/sysConfig'

export default {
	name:'',
	props: {
        closeAddFun: {
            type: Function,
            required: true,
        }
    }, 
    created() {
    	var self = this;

		utils.ilinkArrHandle.delDicArr_keyVal(38, function(arr){
			self.options = arr[0][0]
		})
    },
	data() {
		return {
			options: {},
			attributeForm: {
				id:'',
				name: '',
				code:'',
				unit:'',
			},
			attributeFormRules: {
			    name: [{ 
                    required: true, message: '请输入名称', trigger: 'blur' 
                }],
                code: [{
                	required: true, message: '请输入属性字段', trigger:'blur'
                }],
			}
		}
	},
	methods: {
		saveLinkAttr(formName) {
			var self = this;
			 // 阻止事件冒泡
            event.stopPropagation();

            self.$refs[formName].validate((valid) => {
            	if(valid) {
            		var parames = self.attributeForm;
            		updateFcfNormalP(parames).then(res => {
            		   if(res.status === 200) {
                            self.$message({
                                message: '保存成功！',
                                type: 'success'
                            });
                            self.closeAddFun(true);
                        } else {
                            self.$message({
                                message: res.info,
                                type: 'warning'
                            })
                        }    			
            		});
            	} else {
            		return false;
            	}
            });
		},
		changeItem(selVal) {
			var self = this;
			self.attributeForm.unit = selVal;
		},
	}
}
</script>